<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Dashboard
*/
class Module_spsc_spsc {
	
	private $controller;
	private $ss;
	private $message;
	private $thesystems;
	private $mydatetime;
	private $arraylist;
	
	static function &factory() {
	   $o = new Module_spsc_spsc();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller	= Request::current()->controller();
		$this->ss					= Session::instance();
		$this->thesystems	= Helper_theSystems::factory();
		$this->mydatetime	= Helper_MyDateTime::factory();
		$this->message		= Language_Message::factory();
		$this->arraylist	= Helper_ArrayList::factory();
	}
	
	public function spsc($id, $command)
	{
 		
		$value_rpp		= Arr::get($_REQUEST,'rpp');
		$rpp			= (!empty($value_rpp) ? $value_rpp : '5');
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		
		switch ($command)
		{
		case "search":
				  // Search User
					$nickname      = Arr::get($_POST, 'nickname');
					$nama      = Arr::get($_POST, 'nama');
					$userroles_key = Arr::get($_POST, 'userroles_key');
					$shift         = Arr::get($_POST, 'shift');
          $this->ss->set('kriteria',$_POST);
				  break;
		
		case "viewall":
				  $this->ss->delete('kriteria');
				  $r = Request::current();
				  $r->redirect($this->controller.'/spsc');
				  break;
		
		case "create":
				
					  
					// Create Data
					$id            = $_POST['id'];
					$tgl           = $_POST['tgl'];
					$id_sppsc      = $_POST['id_sppsc'];
				  
				  $query = DB::insert('surat_pembelian_sukucadang', array('id','tgl','id_sppsc'))
				  ->values(array($id,$tgl,$id_sppsc));
				  $result = $query->execute();
					  if ($result[1] == 1) {
						$this->message->store_message('c_success');
						$r = Request::current();
						$r->redirect($this->controller.'/spsc?id='.$id);
					  } else {
						$this->message->store_message('c_fails');
					  }
				  break;

		case "create_child":
				
					  
					// Create Data
					$id_master     = $_GET['id'];
					$id_sukucadang = $_POST['id_sukucadang'];
					$jumlah        = $_POST['jumlah'];
				  
				  $query = DB::insert('surat_pembelian_sukucadang_t', array('id_master','id_sukucadang','jumlah'))
				  ->values(array($id_master,$id_sukucadang,$jumlah));
				  $result = $query->execute();
					  if ($result[1] == 1) {
						$this->message->store_message('c_success');
						$r = Request::current();
						$r->redirect($this->controller.'/spsc?id='.$id_master);
					  } else {
						$this->message->store_message('c_fails');
					  }
				  break;
		
		case "update":
				 
					// Update Data
					$p['id']            = $_POST['id'];
					$p['tgl']           = $_POST['tgl'];
					$p['id_sppsc']      = $_POST['id_sppsc'];
				  
				  $query = DB::update('surat_pembelian_sukucadang')
					  ->set($p)
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('u_success');
						$r = Request::current();
						$r->redirect($this->controller.'/spsc?id='.$id);
					  } else {
						$this->message->store_message('u_fails');
					  }
				  break;
		
		case "delete":
				
				  $query = DB::delete('surat_pembelian_sukucadang')
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('d_success');
						$r = Request::current();
						$r->redirect($this->controller.'/spsc');
					  } else {
						$this->message->store_message('d_fails');
					  }
				  break;

		case "delete_child":
				
				  $query = DB::delete('surat_pembelian_sukucadang_t')
					  ->where('id', '=', Arr::get($_REQUEST,'id_child'))
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
						$this->message->store_message('d_success');
						$r = Request::current();
						$r->redirect($this->controller.'/spsc?id='.$id);
					  } else {
						$this->message->store_message('d_fails');
					  }
				  break;
		
		
		}
		
		/*pencarian start*/
		$key   = Arr::get($_REQUEST,'key');
		$value = Arr::get($_REQUEST,'value');
		/*pencarian end*/

		$all_items = DB::select()
			->from('surat_pembelian_sukucadang');

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$all_items = $all_items->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/

		$all_items = $all_items->execute();
		$count = $all_items->count();
		
		$spsclists = DB::select()
			->from('surat_pembelian_sukucadang')
			->limit($rpp)
			->offset($offset);

		/*pencarian start*/
		if(!empty($key))
		{
			if(!empty($value)) {
				$spsclists = $spsclists->where($key, 'LIKE', '%'.$value.'%');
			}
		}
		/*pencarian end*/
			
		$spsclists = $spsclists->execute();
		
		$spsc_update = DB::select()
			->from('surat_pembelian_sukucadang')
			->where('id', '=', $id)
			->limit(1)
			->execute();

		$spsc_childs = DB::select()
			->from('surat_pembelian_sukucadang_t')
			->where('id_master', '=', $id)
			->execute()->as_array();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'User Administrator';
		$content['content']	= View::factory('inc/spsc')
			->set('spsclists',$spsclists)
			->set('spsc_update',$spsc_update[0])
			->set('pagination',$pagination)
			->set('spsc_childs',$spsc_childs)
			->render();
		
		return $content;
	}
		
	
	
	
}